<template>
  <div class="computer-room-management">
    <el-card class="search-card">
      <el-form :model="searchForm" label-width="80px" inline>
        <el-form-item label="机房名称">
          <el-input v-model="searchForm.name" placeholder="请输入机房名称" />
        </el-form-item>
        <el-form-item label="状态">
          <el-select v-model="searchForm.status" placeholder="请选择状态" clearable>
            <el-option label="启用" value="enabled" />
            <el-option label="禁用" value="disabled" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleSearch">查询</el-button>
          <el-button @click="handleReset">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>

    <el-card>
      <template #header>
        <div class="card-header">
          <div>
            <SvgIcon name="template" />
            <span>机房列表</span>
          </div>
          <el-button type="primary" @click="handleAdd">新增机房</el-button>
        </div>
      </template>

      <el-table :data="tableData" border style="width: 100%">
        <el-table-column prop="id" label="ID" width="80" />
        <el-table-column prop="name" label="机房名称" />
        <el-table-column prop="location" label="位置" />
        <el-table-column prop="manager" label="负责人" />
        <el-table-column prop="capacity" label="容量" />
        <el-table-column prop="status" label="状态" width="80">
          <template #default="scope">
            <el-tag :type="scope.row.status === 'enabled' ? 'success' : 'danger'">
              {{ scope.row.status === 'enabled' ? '启用' : '禁用' }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="createTime" label="创建时间" width="180" />
        <el-table-column label="操作" width="200" fixed="right">
          <template #default="scope">
            <el-button size="small" @click="handleEdit(scope.row)">编辑</el-button>
            <el-button size="small" type="danger" @click="handleDelete(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <div class="pagination-container">
        <el-pagination
          v-model:current-page="pagination.currentPage"
          v-model:page-size="pagination.pageSize"
          :page-sizes="[10, 20, 50, 100]"
          :total="pagination.total"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>

    <!-- 新增/编辑对话框 -->
    <el-dialog
      :title="dialogTitle"
      v-model="dialogVisible"
      width="500px"
      @close="handleDialogClose"
    >
      <el-form
        :model="formData"
        :rules="formRules"
        ref="formRef"
        label-width="100px"
      >
        <el-form-item label="机房名称" prop="name">
          <el-input v-model="formData.name" placeholder="请输入机房名称" />
        </el-form-item>
        <el-form-item label="位置" prop="location">
          <el-input v-model="formData.location" placeholder="请输入位置" />
        </el-form-item>
        <el-form-item label="负责人" prop="manager">
          <el-input v-model="formData.manager" placeholder="请输入负责人" />
        </el-form-item>
        <el-form-item label="容量" prop="capacity">
          <el-input-number
            v-model="formData.capacity"
            :min="1"
            controls-position="right"
            style="width: 100%"
          />
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-radio-group v-model="formData.status">
            <el-radio label="enabled">启用</el-radio>
            <el-radio label="disabled">禁用</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="primary" @click="handleSubmit">确定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive, onMounted } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import SvgIcon from '@/components/SvgIcon.vue'

// 搜索表单数据
const searchForm = reactive({
  name: '',
  status: ''
})

// 表格数据
const tableData = ref([
  {
    id: 1,
    name: 'A机房',
    location: '1号楼1层',
    manager: '张三',
    capacity: 100,
    status: 'enabled',
    createTime: '2023-01-01 12:00:00'
  },
  {
    id: 2,
    name: 'B机房',
    location: '2号楼3层',
    manager: '李四',
    capacity: 80,
    status: 'disabled',
    createTime: '2023-02-01 12:00:00'
  },
  {
    id: 3,
    name: 'C机房',
    location: '3号楼2层',
    manager: '王五',
    capacity: 120,
    status: 'enabled',
    createTime: '2023-03-01 12:00:00'
  }
])

// 分页数据
const pagination = reactive({
  currentPage: 1,
  pageSize: 10,
  total: 3
})

// 对话框相关
const dialogVisible = ref(false)
const dialogTitle = ref('')
const isEdit = ref(false)

// 表单数据
const formData = reactive({
  id: 0,
  name: '',
  location: '',
  manager: '',
  capacity: 10,
  status: 'enabled'
})

// 表单验证规则
const formRules = {
  name: [
    { required: true, message: '请输入机房名称', trigger: 'blur' }
  ],
  location: [
    { required: true, message: '请输入位置', trigger: 'blur' }
  ],
  manager: [
    { required: true, message: '请输入负责人', trigger: 'blur' }
  ],
  capacity: [
    { required: true, message: '请输入容量', trigger: 'blur' }
  ]
}

// 表单引用
const formRef = ref()

// 搜索
const handleSearch = () => {
  ElMessage.success('搜索功能已触发')
  console.log('搜索条件:', searchForm)
}

// 重置搜索
const handleReset = () => {
  searchForm.name = ''
  searchForm.status = ''
  ElMessage.info('已重置搜索条件')
}

// 新增
const handleAdd = () => {
  dialogTitle.value = '新增机房'
  isEdit.value = false
  // 重置表单数据
  Object.assign(formData, {
    id: 0,
    name: '',
    location: '',
    manager: '',
    capacity: 10,
    status: 'enabled'
  })
  dialogVisible.value = true
}

// 编辑
const handleEdit = (row: any) => {
  dialogTitle.value = '编辑机房'
  isEdit.value = true
  // 填充表单数据
  Object.assign(formData, row)
  dialogVisible.value = true
}

// 删除
const handleDelete = (row: any) => {
  ElMessageBox.confirm(
    `确定要删除机房 "${row.name}" 吗？`,
    '确认删除',
    {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
    }
  ).then(() => {
    // 执行删除操作
    ElMessage.success('删除成功')
  }).catch(() => {
    ElMessage.info('已取消删除')
  })
}

// 提交表单
const handleSubmit = () => {
  formRef.value.validate((valid: boolean) => {
    if (valid) {
      if (isEdit.value) {
        // 编辑操作
        ElMessage.success('编辑成功')
      } else {
        // 新增操作
        ElMessage.success('新增成功')
      }
      dialogVisible.value = false
    }
  })
}

// 关闭对话框
const handleDialogClose = () => {
  formRef.value.resetFields()
}

// 分页相关
const handleSizeChange = (val: number) => {
  pagination.pageSize = val
  ElMessage.info(`每页显示 ${val} 条`)
}

const handleCurrentChange = (val: number) => {
  pagination.currentPage = val
  ElMessage.info(`当前页: ${val}`)
}

// 初始化
onMounted(() => {
  ElMessage.success('机房管理页面加载完成')
})
</script>

<style scoped lang="scss">
.computer-room-management {
  .search-card {
    margin-bottom: 20px;
  }

  .card-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
  }

  .pagination-container {
    margin-top: 20px;
    display: flex;
    justify-content: flex-end;
  }
}
</style>